package com.xudow.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.activity.ActivityWithAgency;
import com.xudow.app.R;
import com.xudow.app.ui.task.ActivityDetailTask;
import com.xudow.app.util.Maps;

import java.util.HashMap;

/**
 * 活动详情Activity
 *
 * @author lukecheng
 */
public class ActivityViewActivity extends BaseActivity {

    private Long activityId;
    private String activityPageUrl;
    private String activityTitle;
    private String pageContent;
    private WebView webView;
    private ProgressBar progressBar;
    private TextView title;

    private boolean isLoadFinish = false; //是否加载完成

    private ActivityDetailTask activityDetailTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_view);

        Intent intent = getIntent();
        activityId = intent.getLongExtra("activityId", 0L);
        Log.i("zibianji","pageContent--"+intent.getStringExtra("pagecontent"));
        pageContent = intent.getStringExtra("pagecontent");
        activityPageUrl = intent.getStringExtra("pageurl");
        progressBar = (ProgressBar) findViewById(R.id.progressbar);
        title = (TextView) findViewById(R.id.title);

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                progressBar.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
            }
        });
        if (0 != activityId) {//活动
            title.setText("活动详情");
            HashMap<String, String> params = Maps.newHashMap();
            params.put("activity_id", "" + activityId);

            activityDetailTask = new ActivityDetailTask(this, activityDetailTaskHandler);
            addTask(activityDetailTask);
            activityDetailTask.execute(params);
        } else if (null != activityPageUrl) {//外部链接
            title.setText("外部链接");
            webView.loadUrl(activityPageUrl);
            Log.i("zibianji", "pageContent--" + activityPageUrl);
        } else if (null != pageContent) {//自定义页面
            Log.i("zibianji","pageContent--"+pageContent);
            Log.i("zibianji","pageContent--");
            title.setText("自编辑页面");
            Log.i("zibianji","pageContent--"+pageContent);
            webView.loadDataWithBaseURL("about:blank", pageContent, "text/html", "utf-8", "");
        }
    }

    private Handler activityDetailTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (ActivityDetailTask.SUCCESS == msg.what) {
                ActivityWithAgency ac = (ActivityWithAgency) msg.getData().getSerializable("activity");
                activityPageUrl = ac.getActivityPageUrl();
                activityTitle = ac.getTitle();

                isLoadFinish = true;

                webView.loadUrl(activityPageUrl);
            }
        }
    };

    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 分享按钮点击
     *
     * @param view
     */
    public void onShareClick(View view) {
        if (!isLoadFinish) {
            return;
        }

        //TODO:分享什么?
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
        intent.putExtra(Intent.EXTRA_TEXT, activityTitle);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, getString(R.string.shared_to)));
    }

    /**
     * 立即报名点击事件
     *
     * @param view
     */
    public void onJoinClick(View view) {
    }

    /**
     * 收藏点击事件
     *
     * @param view
     */
    public void onFavoriteClick(View view) {
        //TODO:缺少活动收藏
    }
}
